package cn.pengyinrui.department.system.register.controller;

import cn.pengyinrui.department.constants.BaseConstants;
import cn.pengyinrui.department.system.emp.domain.Employee;
import cn.pengyinrui.department.system.register.domain.Register;
import cn.pengyinrui.department.system.user.service.IUserService;
import cn.pengyinrui.department.utils.AjaxResult;
import com.mysql.jdbc.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/register")
@Api(value = "注册的API",description = "注册相关的功能")
public class RegisterController {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private IUserService userService;


    @PostMapping("/phone")
    // 对方法做描述
    @ApiOperation(value = "管理员注册注册")
    public AjaxResult phone(@RequestBody Register register){
        // 取值
        String email = register.getEmail();
        String emailCode = register.getEmailCode();
        String phone = register.getPhone();
        String phoneCode = register.getPhoneCode();
        String password = register.getPassword();
        String passwordRepeat = register.getPasswordRepeat();
        // 非空判断
        if(StringUtils.isNullOrEmpty(phone)){
            return AjaxResult.error().setTip("电话号码不能为空");
        }
        if(StringUtils.isNullOrEmpty(phoneCode)){
            return AjaxResult.error().setTip("验证码不能为空");
        }
        if(StringUtils.isNullOrEmpty(password)){
            return AjaxResult.error().setTip("密码不能为空");
        }
        if(StringUtils.isNullOrEmpty(passwordRepeat)){
            return AjaxResult.error().setTip("确认密码不能为空");
        }
        // 判断两次密码是否相同
        if(!password.equals(passwordRepeat)){
            return AjaxResult.error().setTip("两次密码不一致");
        }

        // 校验
        // 手机号验证
        if(userService.findOneByPhone(phone) != null){
            return AjaxResult.error().setTip("手机号已注册，请登录或忘记密码");
        }
        // 短信验证
        String key = phone + BaseConstants.Vcode.REGISTER;
        String msg = (String) redisTemplate.opsForValue().get(key);
        if(msg == null){
            return AjaxResult.error().setTip("短信验证码已过期");
        }
        if(!phoneCode.equals(msg)){
            return AjaxResult.error().setTip("短信验证证码错误");
        }

        // 邮箱验证
        if(userService.findOneByEmail(email) != null){
            return AjaxResult.error().setTip("邮箱已注册，请登录或忘记密码");
        }
        // 邮箱码验证
        String emailKey = email + BaseConstants.Vcode.REGISTER;
        String emailMsg = (String) redisTemplate.opsForValue().get(emailKey);
        if(emailMsg == null){
            return AjaxResult.error().setTip("邮箱验证码已过期");
        }
        if(!phoneCode.equals(emailMsg)){
            return AjaxResult.error().setTip("邮箱验证证码错误");
        }

        userService.addByEmail(register);
        return AjaxResult.success();
    }
}
